package com.buba.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.pojo.user.ThePatient;
import com.buba.user.pojoVo.ThePatientVo;
import com.buba.user.service.ThePatientService;
import com.buba.user.service.UserService;
import com.buba.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Calendar;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.pojo.user.PatientCard;
import com.buba.pojo.user.ThePatient;
import com.buba.pojoVo.dataTime;
import com.buba.user.pojoVo.ThePatientVo;
import com.buba.user.service.PatientCardService;
import com.buba.user.service.ThePatientService;
import com.buba.user.service.impl.ThePatientServiceImpl;
import com.buba.user.service.UserService;
import com.buba.utils.R;
import com.buba.utils.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.buba.pojo.user.ThePatient;
import com.buba.user.service.ThePatientService;
import com.buba.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import java.util.Date;
import java.util.List;

import static com.buba.utils.DateUtil.dateToWeek;


/**
 * <p>
 * 就诊人表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-06-09
 */
@RestController
@RequestMapping("/user/the-patient")
@Slf4j
public class ThePatientController {
    @Autowired
    private ThePatientService thePatientService;

    @Autowired
    private PatientCardService patientCardService;

    //修改
    @PutMapping("/updatethePatient")
    public R updatUser(@RequestBody ThePatient thePatient){
        boolean update=thePatientService.updateById(thePatient);
        if(update==true){
            return R.ok().data("ok","修改成功");
        }else {
            return R.ok().data("error","修改失败");
        }
    }



    @Autowired
    private UserService userService;
    //删除就诊人
    @GetMapping("/patientDelById/{id}")
    public R patientDelById(@PathVariable Integer id){
        log.info("patientDelById:::"+id);
        boolean b = thePatientService.removeById(id);
        if (b){
            return R.ok().data("message" , "删除成功");
        }else{
            return R.error().data("message" , "删除失败");
        }
    }

    //修改就诊人
    @PostMapping("/patientUpdateById")
    public R patientUpdateById(@RequestBody ThePatient patient){
        boolean b = thePatientService.updateById(patient);
        if (b){
            return R.ok().data("message" , "修改成功");
        }else{
            return R.error().data("message" , "修改失败");
        }
    }


    //修改就诊人回显
    @GetMapping("/patientById/{id}")
    public R patientById(@PathVariable Integer id){
        QueryWrapper<ThePatient> wrapper = new QueryWrapper<>();
        ThePatient patient = thePatientService.getById(id);
        return R.ok().data("patient" , patient);
    }


    //添加就诊人
    @PostMapping("/patientSave")
    public R patientSave(@RequestBody ThePatient patient){
        log.info("patient:::"+patient);

        QueryWrapper<ThePatient> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id" , patient.getUserId());

//        List<ThePatient> list = thePatientService.list(wrapper);
//        if (list.size() == 4){
//            return R.ok().data("message" , "添加失败");
//        }

        if (patient.getDefaultPerson().equals("是")){
            wrapper.eq("default_person", "是");
            ThePatient thePatient = thePatientService.getOne(wrapper);
            log.info("thePatient:::"+thePatient);
            if (thePatient != null){
                thePatient.setDefaultPerson("否");
                thePatientService.updateById(thePatient);
            }
        }

        patient.setHospitalNumber(RandomUtil.getSixBitRandom());
        patient.setHospitalization("否");
        Calendar calendar = Calendar.getInstance();//获取时间
        patient.setCreationTime(calendar.getTime());
        patient.setModificationTime(calendar.getTime());

        PatientCard card = new PatientCard();
        card.setUserId(patient.getUserId());
        card.setCardNo(patient.getHospitalNumber());
        card.setBalance(BigDecimal.valueOf(0));

        patientCardService.save(card);

        boolean save = thePatientService.save(patient);
        if (save){
            return R.ok().data("message" , "添加成功");
        }else{
            return R.error().data("message" , "添加失败");
        }
    }

    //根据用户id获取就诊人信息
    @GetMapping("/patientByUserId/{id}")
    public R patientByUserId(@PathVariable Integer id){
        log.info("id:::"+id);
        QueryWrapper<ThePatient> wrapper = new QueryWrapper<>();
        if (id != null) {
            wrapper.eq("user_id" , id);
        }
        List<ThePatient> patientList = thePatientService.list(wrapper);
        return R.ok().data("patientList" , patientList);
    }


    /**
     * todo 查询同一个user账号下的病人列表
     * @param userId
     * @return com.buba.utils.R
     * @author xhh
     * @data 2022/6/10 14:38
     */
    @RequestMapping(value = "/findPatientByUserId",method = RequestMethod.POST)
    public R findPatientByUserId(@RequestParam("userId")Integer userId){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        List<ThePatient> list = thePatientService.list(queryWrapper);
        for (ThePatient thePatient : list) {
            //thePatient.getNumber().replaceAll()
        }
        return R.ok().data("thePatientList",list);
    }


    /*
     * @Author: 张庆威
     * @Description: (获取就诊人)
     * @Date: 11:41 2022/6/14
     * @Param: [userId]
     * @return: com.buba.utils.R
     **/
    @GetMapping({"/getThePatientById/{userId}"})
    public R getThePatientById(@PathVariable("userId") Integer userId) {
        System.out.println("进入");
        ThePatientVo thePatient = this.thePatientService.getThePatientByIdOne(userId);
        List<ThePatientVo> list = this.thePatientService.getThePatientById(userId);
        System.out.println(thePatient);
        System.out.println(list);
        return R.ok().data("thePatient", thePatient).data("list", list);
    }

    /*
     * @Author: 张庆威
     * @Description: (根据当前日期 获取当前一周的日期 与 周几)
     * @Date: 11:43 2022/6/14
     * @Param: []
     * @return: com.buba.utils.R
     **/
    @GetMapping({"/getDates"})
    public R getDates() {
        //一周的日期 与 周几
        List<dataTime> list = new ArrayList<>();
        // 定义输出日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("dd");
        Date currentDate = new Date();

        //当天
        dataTime today = new dataTime();
        today.setTime(currentDate); //当前日期
        today.setNumber(sdf2.format(currentDate));
        today.setDay(dateToWeek(sdf.format(currentDate)));


        List<Date> days = dateToWeek(currentDate);
        log.info("今天的日期: " + sdf.format(currentDate));
        for (Date date : days) {
            dataTime dataTime = new dataTime();
            dataTime.setNumber(sdf2.format(date));
            dataTime.setTime(date);
            dataTime.setDay(dateToWeek(sdf.format(date)));
            list.add(dataTime);
        }

        return R.ok().data("list",list).data("today",today);
    }

}

